using System;
using System.Collections;
using System.Collections.Generic;
using Commons;
using Datas;
using Mgrs;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UIElements;
using Button = UnityEngine.UI.Button;
using Image = UnityEngine.UI.Image;

public class MainScene : ComMonoBehaviour
{
    public TextMeshProUGUI textName;
    public TextMeshProUGUI textPrestige;
    public TextMeshProUGUI textMoney;
    
    public Button btnSet;
    public Button btnKit;
    
    private void Awake()
    {
        btnSet.onClick.AddListener(OnBtnSet);
        btnKit.onClick.AddListener(OnBtnKit);
    }

    // Start is called before the first frame update
    void Start()
    {
        AddListeners();
        UpdateLayer();
        
        PanelMgr.OpenRoadPanel(transform);
    }

    /**添加监听 */
    private void AddListeners()
    {
        RegisterMsg(MsgData.ChangePrestige, OnChangePrestige);
        RegisterMsg(MsgData.ChangeMoney, OnChangeMoney);
    }

    protected override void OnBeforeDestroy()
    {
    }

    // Update is called once per frame
    void Update()
    {

    }

    /**刷新界面 */
    private void UpdateLayer()
    {
        textName.text = PlayerData.GetName();
        textPrestige.text = PlayerData.GetPrestige() + "";
        textMoney.text = PlayerData.GetMoney() + "";
    }
    
    private void OnBtnSet()
    {
       PanelMgr.OpenSetPanel(transform);
    }
    
    private void OnBtnKit()
    {
        PanelMgr.OpenKitPanel(transform);
    }

    private void OnChangePrestige(object data = null)
    {
        textPrestige.text = PlayerData.GetPrestige() + "";
        
        if (PlayerData.IsFail())
        {
            Debug.Log("游戏结束");
            SceneManager.LoadScene("StartScene");
        }
    }
    private void OnChangeMoney(object data = null)
    {
        Debug.Log("修改金钱");

        textMoney.text = PlayerData.GetMoney() + "";
    }
}
